//
//  DHSearchViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/11.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSearchViewController.h"
#import "DHSearchView.h"
#import "DHShoppingCartViewController.h"

@interface DHSearchViewController ()

@property (weak, nonatomic) IBOutlet UIButton *cartNumberButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *navViewTopLayout;

@property (strong, nonatomic) DHSearchView *searchView;


@end

@implementation DHSearchViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self loadCartNumber];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([UIDevice systemVersion] < 11) {
        self.navViewTopLayout.constant = 20;
    }
    
    [self.cartNumberButton setTitle:[NSString stringWithFormat:@"%ld",[GlobalManager shareGlobalManager].cartNumber] forState:UIControlStateNormal];
    
    self.searchView = [[DHSearchView alloc] initWithFrame:CGRectMake(0, self.navViewTopLayout.constant + 44, kScreenWidth, kScreenHeight - self.navView.bottom)];
    [self.searchView loadConditionData];
    [self.view addSubview:self.searchView];
}

- (void)viewDidLayoutSubviews{
    self.searchView.frame = CGRectMake(0, self.navView.bottom, kScreenWidth, kScreenHeight - self.navView.bottom);
}

#pragma mark - Load Data
// 获取当前购物车数量
- (void)loadCartNumber{
    if ([Login isLogin]) {
        [[DHNetAPIClient shareDHNetAPIClient] request_cart_countAndBlock:^(id data, NSError *error) {
            [self.cartNumberButton setTitle:[NSString stringWithFormat:@"%ld",[[data objectForKey:@"count"] integerValue]] forState:UIControlStateNormal];
        }];
    }
}

// 返回
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

// 进入购物车
- (IBAction)cartAction:(id)sender {
    if ([Login isLogin]) {
        DHShoppingCartViewController *cartController = [[DHShoppingCartViewController alloc] init];
        [self.navigationController pushViewController:cartController animated:YES];
    }else{
        [kAppDelegate configLoginController];
    }
}


@end
